package com.be.beadmin.project.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.project.dto.input.ProductTotalInfoDto;
import com.be.beadmin.project.dto.output.ProductTotalInfoEx;
import com.be.beadmin.project.service.ProductTotalInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author zjm
* @date 2023-02-14
**/
@Api(tags = {"项目管理-柜体总体信息模块"})
@RestController
public class ProductTotalInfoController {

    @Autowired
    private  ProductTotalInfoService productTotalInfoService;

    @ApiOperation("分页获取柜体总体信息")
    @PostMapping(value = "/project/getProductTotalInfoByPage")
    public ResultModel getProductTotalInfoByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(productTotalInfoService.getProductTotalInfoByPage(pageEntity)) ;
    }
    @ApiOperation("添加一条柜体总体信息")
    @PostMapping("/project/addProductTotalInfo")
    public ResultModel addProductTotalInfo(@RequestBody ProductTotalInfoDto productTotalInfoDto){
        productTotalInfoService.addProductTotalInfo(productTotalInfoDto);
        return ResultModel.success(ResultModel.ADD);
    }
    @ApiOperation("删除一条柜体总体信息")
    @PostMapping("/project/delProductTotalInfo")
    public ResultModel delProductTotalInfo(@RequestBody ProductTotalInfoDto productTotalInfoDto){
        productTotalInfoService.delProductTotalInfo(productTotalInfoDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }

    @ApiOperation("更新一条柜体总体信息")
    @PostMapping("/project/updateProductTotalInfo")
    public ResultModel updateProductTotalInfo(@RequestBody ProductTotalInfoDto productTotalInfoDto){
        productTotalInfoService.updateProductTotalInfo(productTotalInfoDto);
        return ResultModel.success(ResultModel.UPDATE);
    }

    @ApiOperation("导出柜体总体信息")
    @PostMapping("/project/exportProductTotalInfo")
    public void exportProductTotalInfo(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<ProductTotalInfoEx> productTotalInfoPage = productTotalInfoService.getProductTotalInfoByPage(pageEntity);
        List<ProductTotalInfoEx> productTotalInfoExes = productTotalInfoPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("ProductTotalInfo接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ProductTotalInfoEx.class).sheet("sheet1").doWrite(productTotalInfoExes);
    }
}